package com.sugon.entity;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * 接口响应结果
 * @description:
 * @author: YuXD
 * @create: 2018-12-16 17:00
 **/

@JsonInclude(JsonInclude.Include.NON_NULL)
@ApiModel("接口响应结果")
@Data
public class ApiResult<T> implements Serializable {

    private static final long serialVersionUID = -8810954049370861490L;

    @ApiModelProperty(value = "业务状态码")
    public int status;
    @ApiModelProperty(value = "响应结果")
    public String msg;
    @ApiModelProperty(value = "返回数据")
    public T data;

    public ApiResult() {
        super();
    }

    /**
     *
     * @param status
     * @param msg
     * @param result
     */
    public ApiResult(int status, String msg, T result) {
        super();
        this.status = status;
        this.msg = msg;
        this.data = result;
    }

    /**
     *
     * @param result
     */
    public ApiResult(T result) {
        super();
        this.status = Ar.SUCCESS.getCode();
        this.msg = Ar.SUCCESS.getMsg();
        this.data = result;
    }

    /**
     *
     * @param result
     * @return
     */
    public ApiResult<T> success(T result) {
        this.status = Ar.SUCCESS.getCode();
        this.msg = Ar.SUCCESS.getMsg();
        this.data = result;
        return this;
    }

    /**
     *
     * @return
     */
    public ApiResult<T> success() {
        this.status = Ar.SUCCESS.getCode();
        this.msg = Ar.SUCCESS.getMsg();
        return this;
    }

    /**
     *
     * @param msg
     * @return
     */
    public ApiResult<T> succMsg(String msg) {
        this.status = Ar.SUCCESS.getCode();
        if (StrUtil.isEmpty(msg)) {
            this.msg = Ar.SUCCESS.getMsg();
        } else {
            this.msg = msg;
        }
        return this;
    }

    /**
     *
     * @param msg
     * @return
     */
    public ApiResult<T> dataError(String msg) {
        this.status = Ar.DATAERROR.getCode();
        if (StrUtil.isEmpty(msg)) {
            this.msg = Ar.DATAERROR.getMsg();
        } else {
            this.msg = msg;
        }
        return this;
    }

    /**
     *
     * @param msg
     * @return
     */
    public ApiResult<T> failMsg(String msg) {
        this.status = Ar.FAIL.getCode();
        if (StrUtil.isEmpty(msg)) {
            this.msg = Ar.FAIL.getMsg();
        } else {
            this.msg = msg;
        }
        return this;
    }

    /**
     *
     * @param msg
     * @return
     */
    public ApiResult<T> fail(String msg) {
        this.status = Ar.FAIL.getCode();
        if (StrUtil.isEmpty(msg)) {
            this.msg = Ar.FAIL.getMsg();
        } else {
            this.msg = msg;
        }
        return this;
    }

    /**
     *
     * @return
     */
    public ApiResult<T> fail() {
        this.status = Ar.FAIL.getCode();
        this.msg = Ar.FAIL.getMsg();
        return this;
    }

    /**
     *
     * @return
     */
    public ApiResult<T> unauthorized() {
        this.status = Ar.UNAUTHORIZED.getCode();
        this.msg = Ar.UNAUTHORIZED.getMsg();
        return this;
    }

    /**
     *
     * @return
     */
    public ApiResult<T> invalid() {
        this.status = Ar.INVALID.getCode();
        this.msg = Ar.INVALID.getMsg();
        return this;
    }

}
